package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vo.AdUserVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;


import com.heima.utils.common.AppJwtUtil;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;

@Service
public class AdUserServiceimpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto)  {
        //判断用户是否存在
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户信息不存在");
        }
        //账户 密码不能为空
        if(StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"账号或密码错误");
        }

        LambdaQueryWrapper<AdUser> wrapper = Wrappers.lambdaQuery();
        AdUser user = getOne(wrapper.eq(AdUser::getName, dto.getName()));
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"账户不存在");
        }
        String inputPwd= DigestUtils.md5DigestAsHex((dto.getPassword()+ user.getSalt()).getBytes());
        if(!inputPwd.equals(user.getPassword())){
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //判断用户状态
        if(user.getStatus()!=9){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"用户状态异常");
        }
        //修改登录时间
        user.setCreatedTime(new Date());

        //办发token
        String token = AppJwtUtil.getToken(user.getId().longValue());
        //创建对象封装  封装返回结果   {user: {用户信息}  ,  token: "令牌凭证" }
        AdUserVO userVO = new AdUserVO();

        BeanUtils.copyProperties(user,userVO);

        HashMap map = new HashMap();
        map.put("token",token);
        map.put("user",userVO);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult register(AdUser user) {
        //判断user是否为空
        if (StringUtils.isEmpty(user.getName())||StringUtils.isEmpty(user.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名或密码不能为空");
        }

        //判断账户是否存在
        LambdaQueryWrapper<AdUser> wrapper = Wrappers.lambdaQuery();
         if (user.getName().equals( wrapper.eq(AdUser::getName, user.getName()))){
             return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名已存在");
         }


         //采用加盐算法加密密码
        String salt = RandomStringUtils.randomAlphanumeric(8);
        String pwd= user.getPassword();
        //加盐密码
        user.setPassword(DigestUtils.md5DigestAsHex((pwd+salt).getBytes()));
        //盐算法
        user.setSalt(salt);
        //创建日期
        user.setCreatedTime(new Date());
        //设置状态码
        user.setStatus(9);
        //保存注册用户
        save(user);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
